package com.woniuxy.yoga.commons.core.util;

import com.woniuxy.yoga.commons.core.exception.YogaException;
import com.woniuxy.yoga.commons.core.exception.token.TokenException;
import com.woniuxy.yoga.commons.core.exception.token.TokenExceptionCode;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JWTUtil {

    //自定义的秘钥
    //HS256：即字节长度>=32

    /**
     * 管理员密钥
     */
    private static final String adminSect = "jljdslfjisdjflksjdlkfjsdijflisdfjlkksjflksjdlkfjsdlks";
    /**
     * 商家密钥
     */
    private static final String businessSect = "sdfwetsdfwetwawrdwraaskjfioewlasdnfoaihflkdnosaihgio";
    /**
     * 教练密钥
     */
    private static final String coachSect = "dfhqwuoihksadjhoweiuhrkjdfnilewuthlkuaheefiuhsirluh";
    /**
     * 用户密钥
     */
    private static final String userSect = "kasjhoqwrlmnmnfalskfiawhfandlksfauldksanfoiaweflcvmsak";



    //生成token
    public static String createToken(Map<String, Object> body, Integer time, String sect) {
        if (body == null || body.size() == 0) throw new TokenException(TokenExceptionCode.TOKEN_EXCEPTION_CODE_NULL);
        Date date = null;
        if (time == null) {
            date = new Date(System.currentTimeMillis()+30*60*1000);
        }else {
            date = new Date(System.currentTimeMillis()+time*60*1000);
        }
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.builder().setClaims(body).setExpiration(date).signWith(key).compact();
    }

    /**
     * 生成Token
     *
     * @param body
     * @param secret
     * @return
     */
    public static String createToken(Map<String, Object> body, String secret) {
        return createToken(body, null, secret);
    }

    //解析token
    public static Claims parseToken(String token,String sect) {
        if (token==null) throw new TokenException(TokenExceptionCode.TOKEN_EXCEPTION_CODE_NULL);
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }

    //生成用户端token
    public static String createUserToken(Map<String, Object> body, Integer time){
        return createToken(body,time,userSect);
    }

    //生成教练端token
    public static String createCoachToken(Map<String, Object> body, Integer time){
        return createToken(body,time,coachSect);
    }

    //生成商家端token
    public static String createBusinessToken(Map<String, Object> body, Integer time){
        return createToken(body,time,businessSect);
    }

    //生成管理端token
    public static String createAdminToken(Map<String, Object> body, Integer time){
        return createToken(body,time,adminSect);
    }

    //解析用户端token
    public static Claims parseUserToken(String token) {
        return parseToken(token,userSect);
    }

    //解析教练端token
    public static Claims parseCoachToken(String token) {
        return parseToken(token,coachSect);
    }

    //解析商家端token
    public static Claims parseBusinessToken(String token) {
        return parseToken(token,businessSect);
    }

    //解析管理端token
    public static Claims parseAdminToken(String token) {
        return parseToken(token,adminSect);
    }
    //获取token里的用户id
    public static Integer getUserId(String token){
        Claims claims = parseUserToken(token);
        return Integer.parseInt(claims.get("userId").toString());
    }

    /**
     * 获取教练ID
     * @param token
     * @return
     */
    public static Integer getCoachId(String token){
        return parseCoachToken(token).get("coachId", Integer.class);
    }

    /**
     * 获取管理员ID
     * @param token
     * @return
     */
    public static Integer getAdminId(String token){
        return parseAdminToken(token).get("accountId", Integer.class);
    }

    /**
     * 获取管理员账号名
     * @param token
     * @return
     */
    public static String getAdminName(String token){
        return parseAdminToken(token).get("accountName", String.class);
    }

    /**
     * 获取商家账号ID
     * @param token
     * @return
     */
    public static Integer getBusinessId(String token){
        return parseBusinessToken(token).get("businessId", Integer.class);
    }

    /**
     * 获取商家账号名
     * @param token
     * @return
     */
    public static String getBusinessName(String token){
        return parseBusinessToken(token).get("businessName", String.class);
    }
}
